package toy.keli.picmap.model;

import java.util.HashMap;

import toy.keli.picmap.model.types.MapPosIn;
import toy.keli.picmap.tools.Control;
import toy.keli.picmap.util.utilArray;

/**
 * 地图操作类
 */
public class map {
    public static def getPoints(MapPosIn pos){
        double[] n = pos.range;
        //范围太大就不要搜索直接返回了
        if(n[1]-n[0]>3) return new def();
        String where= String.format("x>%s and x<%s and y>%s and y<%s", n[0]-0.1,n[1]+0.1,n[2]-0.1,n[3]+0.1);
        if(Control.date[0]>0)
            where+=String.format(" and intime>'%s' and intime<'%s' ",Control.date[0],Control.date[1]);
        if(pos.pid>0)  where += String.format(" and `pid`='%s'" ,pos.pid);
        def keys =new def.list("name","time","path");
        Reflection r=new Reflection(pos);
        for(def name : keys){
            def v=r.getField(name.toString());
            if(!v.isEmpyt())
            where += String.format(" and `%s` like '"+"%"+"%s"+"%"+"'" ,name,v);
        }
        if(pos.key!=null && pos.key.length()>0)
            where += " and (`name` like '%"+pos.key+"%' or  `time` like '%"+pos.key+"%')";
        def a=new pic().find().where(where).order("intime desc").limit(1000).all();

        return a;
    }

    public static HashMap<String,Double> getPostion(long pid){
        HashMap<String,Double> rt = new HashMap<String,Double>();
        rt.put("x",114.06153);
        rt.put("y",22.527225);
        rt.put("z",12.5);
        String where=(pid==0) ? "": "pid="+pid;
        pic p= (pic) new pic().findOne(where);
        if(p.id!=0) {
            rt.put("x", p.x);
            rt.put("y", p.y);
        }
        return rt;
    }

    public static def openId(String id){
        String ids= utilArray.idunzip(id);
        return new pic().find().where("id in ("+ids+")").order("intime desc").all();
//        T rt=db.query("pic","id in ("+ids+")");
//        return rt;
    }

    public static void getCityInfo(){

    }
}
